package com.ccl.iot.device;

import android.graphics.Bitmap;

import com.ccl.iot.IOTObject;
import com.ccl.iot.dvr.utils.Consts;
import com.ccl.iot.dvr.utils.HDImageManager;
import com.ccl.iot.managers.DeviceManager;
import com.ccl.iot.object.IOTDeviceInfoEx;

public abstract class IOTAirCondition extends IOTDeviceWithState implements SwitchDevice, IOTObject.IOTDVRInner {

    //每台空调的寄存器信息
    protected byte[] mRegState;
    private String mUdid = null;

    public IOTAirCondition(long aDevID, IOTDeviceInfoEx aDeviceInfo, int aStateAddress, int aStateSize) {
        super(aDevID, aDeviceInfo, aStateAddress, aStateSize);
        mRegState = new byte[(int) aDeviceInfo.iRegisterSpace];
    }

    protected void setStateAddress(int aStateAddress) {
        iStateAddress = aStateAddress;

        mDefIndex = (iStateAddress - IOTDeviceConst.TDEVREG_ADDRESS.EDEVREG_ADDRESS_POWER);

        if (mDefIndex > 0) {
            mDefIndex /= getDeviceInfo().iRegisterSpace;
        }
        mDefIndex++;
    }

    @Override
    public void updateRegister(int aAddress, int aSize, byte[] aData, int aDataPos, int aNetWork, long time) {
//        saveRegister(aAddress, aSize, aData, aNetWork);
        //		super.updateRegister(aAddress, aSize, aData, aDataPos, aNetWork, time);
//        Log.e("TestRegLog", "name: " + getName() + ", address: " + aAddress + ", size: " + aSize + ", " + "data size: " + aData.length + ", " + "aDataPos: " + aDataPos + ", " + "aNetWork: " + aNetWork + "[" + Arrays.toString(Arrays.copyOf(aData, aSize)) + "]");
        synchronized (mRegState) {
            //当回调的数据和当前数据的长度一样就直接替换
            if (mRegState == null || mRegState.length == aData.length) {
                mRegState = aData;
            } else {
                //当长度不一样就只替换 回调那一部分
                for (int i = 0; i < aData.length; i++) {
                    try {
                        mRegState[aAddress - getRegisterAddress() + i] = aData[i];
                    } catch (Exception e) {
                    }
                }
            }

            if (super.isOnline() != isOnline()) {
                super.setOnline(isOnline());
                DeviceManager.GetDefaultManager().refresh(this);
            }
        }

        afterStateChange();
    }

    //	public byte[] getDatas() {
    //		return mRegState;
    //	}

    @Override
    public String[] getStateIDs() {
        return null;
    }

    @Override
    public int getImageId() {
        if(isOn()){
            return DeviceIconProvider.ICON_LIST_AIR_OPEN[0];
        }else {
            return DeviceIconProvider.ICON_LIST_AIR_CLOSE[0];
        }
    }

    /**
     * 增加设定温度
     */
    public abstract void addTemp();

    /**
     * 降低设定温度
     */
    public abstract void reduceTemp();

    /**
     * 设置设定温度
     */
    public abstract void setTemp(int temp);

    /**
     * 设置模式
     */
    public abstract void setMode(int mode);

    /**
     * 修改风速
     */
    public abstract void setSpeed(int speed);

    /**
     * 修改风向
     */
    public abstract void setDirection(int direction);

    /**
     * 修改模式
     */
    public abstract void modifyMode();

    /**
     * 修改风速
     */
    public abstract void modifySpeed();

    /**
     * 修改风向
     */
    public abstract void modifyDirection();

    /**
     * 获取运行模式
     */
    public abstract int getMode();

    /**
     * 获取风速
     */
    public abstract int getSpeed();

    /**
     * 获取风向
     */
    public abstract int getDirection();

    /**
     * 获取设定温度
     */
    public abstract int getTemp();

    /**
     * 获取环境实际温度
     */
    public abstract int getRealTemp();

    @Override
    public byte[] getRegister() {
        return mRegState;
    }

    @Override
    public long getID() {
        return -super.getID();
    }

    @Override
    public String getUDID() {
        if(mUdid == null){
            mUdid = super.getUDID().replace('-', '@');
        }
        return mUdid;
    }

    @Override
    public boolean setImage(Bitmap bitmap) {
//        String imageID = getImageID(Consts.AIR);
//        getImageCount();
//        if (aImage == null) {
//            if (ImageManager.RemoveBitmap(imageID)) {
//                return true;
//            }
//        } else {
//
//            ImageManager.SetBitmap(imageID, aImage);
//            return true;
//        }
//        return false;

        String imageID = getImageID(Consts.AIR);
        if (bitmap != null){
            return HDImageManager.getInstince().setBitmap(imageID,bitmap);
        }

        return false;
    }

    @Override
    public Bitmap getImage() {

//        String tID = getImageID(Consts.AIR);
//        Bitmap tBitmap = ImageManager.GetBitmap(tID);
//        return tBitmap;

        String imageID = getImageID(Consts.AIR);
        return HDImageManager.getInstince().getBitmap(imageID);

    }

    public String getImagePath(){
        String imageID = getImageID(Consts.AIR);
        return HDImageManager.getInstince().getBitmapPath(imageID);
    }

    @Override
    public int getImageCount() {

//        if (ImageManager.ContainsBitmap(getImageID(Consts.AIR))) {
//            return 1;
//        }
//        return 0;

        if (HDImageManager.getInstince().containsBitmap(getImageID(Consts.AIR))){
            return 1;
        }
        return 0;
    }

    @Override
    public void onViewClick() {
        turn(!isOn());
    }
}
